<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\controller\Common;
use think\Db;

class Withdraw extends Common
{
    /**
     * User:Simony
     * Function: 提现管理
     * @return mixed
     */
    public function index()
    {

        //params
        $field = input('get.field', '');
        $value = input('get.value', '');
        $search_field = input('get.search_field', '');
        $search_value = input('get.search_value', '');

        //where
        $where = [];

        //judge
        if ($field) $where[$field] = $value;
        if ($search_field) $where[$search_field] = $search_value;

        //model
        $withdrawModel = model('common/withdraw/Withdraw');

        //getData
        $data = $withdrawModel->getWithdrawList($where, 10, 'w.*,m.nickname,m.username');

        //assign
        $this->assign('data', $data);
        $this->assign('paymethods', $withdrawModel->paymethods);
        $this->assign('states', $withdrawModel->states);
        $this->assign('page', $withdrawModel->page_info->render());
        return $this->fetch();
    }


    /**
     * User:Simony
     * Function: 处理
     */
    public function handle()
    {

        //params
        $id = input('get.id/d', '');

        //model
        $withdrawModel = model('common/withdraw/Withdraw');
        $withdraw = $withdrawModel->getInfoByID($id);

        if (request()->isPost()) {

            //post params
            $id = input('post.id/d', '');
            $state = input('post.state/d', 0);
            $reason = input('post.reason', '');

            //model or logic
            $withdrawLogic = model('common/withdraw/WithdrawLogic', 'logic');

            Db::startTrans();
            $res = $withdrawLogic->handleWithdrawOp($id, $state,$reason);
            $res['code'] == 'ret' ? Db::commit() : Db::rollback();
            ds_json_encode($res['code'], $res['msg']);
        }
        $this->assign('data', $withdraw);
        $this->assign('states', $withdrawModel->states);
        return $this->fetch();
    }


}